package com.grablist.feihu.grablist.ui.actviity.user.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.grablist.feihu.grablist.base.BaseBean;
import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.bean.ClueInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.ui.actviity.user.biz.CollectionBiz;
import com.grablist.feihu.grablist.ui.actviity.user.contract.CollectionContract;

/**
 * Created by Administrator on 2017/10/21.
 */

public class CollectionPresenter implements CollectionContract.Presenter {

    CollectionContract.View view;
    CollectionBiz collectionBiz;

    private int index = 1;
    List<ClueInfo> clueInfoList = new ArrayList<>();


    @Inject
    public CollectionPresenter(CollectionContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        collectionBiz = (CollectionBiz) biz;
    }

    @Override
    public void getCollection() {
        view.loading(true);
        collectionBiz.getColl(MyApplication.userInfo.getUserCode(), index, Constants.PAGESIZE, new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
            @Override
            public void onSuccess(BaseBean<List<ClueInfo>> o) {
                clueInfoList.addAll(o.getList());
                view.updateCollection(clueInfoList);
                if (index > 1) {
                    view.loadMoreComplete();
                } else {
                    view.refreshComplete();
                }
                view.loading(false);
                view.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                index++;


            }

            @Override
            public void onFailure(BaseBean<List<ClueInfo>> o) {
                view.loading(false);
            }
        });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        view.updateCollection(clueInfoList);
        view.hasLoadMore(false);
    }

}
